//
//  OtherCostClassViewController.swift
//  HuaYun
//
//  Created by enghou on 2019/1/15.
//  Copyright © 2019 xyxorigation. All rights reserved.
//

import UIKit

class OtherCostClassViewController: RefreshedTableViewController {

    
    let typeField = UITextField()
    var types: [JSON] {
        get{
            let mainwindow = UIApplication.shared.delegate?.window ?? UIWindow()
            if let maintab = mainwindow?.rootViewController as? MainTabBarController {
                return maintab.types
            }
            return []
        }
    }
    var selectedIndex = 0
    override func viewDidLoad() {
        super.viewDidLoad()
        self.tableView.tableHeaderView = nil
        self.tableView.separatorStyle = .none
        self.tableView.register(UINib.init(nibName: "CostTableViewCell", bundle: Bundle.main), forCellReuseIdentifier: "CostTableViewCell")
        let header = UIView()
        header.frame = CGRect.init(origin: .zero, size: CGSize(width: self.view.width, height: 40))
        header.backgroundColor = UIColor.white
        typeField.rightimaged = true
        header.addSubview(typeField)
        typeField.font = UIFont.systemFont(ofSize: 14)
        typeField.layer.masksToBounds = true
        typeField.layer.cornerRadius = 3
        typeField.layer.borderWidth = 0.5
        typeField.layer.borderColor = UIColor.tableSeparator.cgColor
        typeField.snp.makeConstraints { (make) in
            make.center.equalToSuperview()
            make.width.equalToSuperview().offset(-30)
            make.height.equalToSuperview().offset(-10)
        }
        if self.types.count > selectedIndex {
            let item = self.types[selectedIndex]["name"].string ?? ""
            typeField.text = item
        }
        let button = UIButton.init(type: .system)
        header.addSubview(button)
        button.addTarget(self, action: #selector(popshowtype), for: .touchUpInside)
        button.snp.makeConstraints { (make) in
            make.center.equalTo(typeField.snp.center)
            make.size.equalTo(typeField.snp.size)
        }
        self.tableView.tableHeaderView = header
    }
    
    override func param(for name: String) -> Dictionary<String, Any> {
        if self.types.count > selectedIndex {
            let item = self.types[selectedIndex]["type"].string ?? ""
            return super.param(for: name) + ["type":item]
        }
        return super.param(for: name)
    }
    
    override func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return 0.0
    }
    
    @objc func popshowtype(){
        let alert = UIAlertController.init(title: nil, message: nil, preferredStyle: .actionSheet)
        
        for item in self.types {
            let action = UIAlertAction.init(title: item["name"].string, style: .default) { (a) in
                self.selectedIndex = self.types.firstIndex(of: item) ?? 0
                self.typeField.text = item["name"].string
                self.beginRefresh()
            }
            alert.addAction(action)
        }
        
        let cancel = UIAlertAction.init(title: "取消", style: .cancel) { (al) in
            
        }
        alert.addAction(cancel)
        
        self.present(alert, animated: true, completion: nil)
    }
    
    override func path(for name: String) -> String {
        return OtherComprehensiveList
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 90
    }
    
    func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
        if let cel = cell as? CostTableViewCell {
            cel.model = self.source[indexPath.row]
        }
    }
    
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        return tableView.dequeueReusableCell(withIdentifier: "CostTableViewCell")!
    }
    
    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        let item = self.source[indexPath.row]
        let dev = DevelopDetailViewController()
        dev.title = "综合计划详情"
        dev.id = item["id"].string ?? ""
        self.push(destination: dev)
    }

}
